package com.qianyu.project.web.controller.lyz;

import com.qianyu.project.entity.MaterialRefoundRecord;
import com.qianyu.project.entity.MaterialRefountRecordDetail;
import com.qianyu.project.service.IMaterialRefoundRecordService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/16 18:43
 */
@RestController
@RequestMapping("/MaterialRefoundRecord")
public class MaterialRefoundRecordController {
    @Autowired
    private IMaterialRefoundRecordService service;

    @GetMapping("")
    public Result getRefoundList(@RequestParam Map<String,String> map){
        return ResultTool.success(service.list());
    }

    @PostMapping("")
    public Result addRefound(@RequestBody Map<String,Object> map){
        Map<String,Object> rMap= (Map<String, Object>) map.get("refound");
        List<Map<String,Object>> dmaplist= (List<Map<String, Object>>) map.get("details");

        MaterialRefoundRecord record=new MaterialRefoundRecord();
        record.setFlowId(1);
        record.setProId(Integer.parseInt(rMap.get("pId").toString()));
        record.setOutstorageName(rMap.get("outstorageName").toString());
        record.setApplyer(rMap.get("applyer").toString());
        record.setDate(rMap.get("date").toString());
        record.setGetUser(Integer.parseInt(rMap.get("getUser").toString()));
        record.setGetOrgId(Integer.parseInt(rMap.get("getOrgId").toString()));
        record.setStorageId(Integer.parseInt(rMap.get("storageId").toString()));
        record.setComment(rMap.get("comment").toString());
        record.setApproveStatus(1);

        List<MaterialRefountRecordDetail> detailList=new ArrayList<MaterialRefountRecordDetail>();
        dmaplist.forEach(d->{
            MaterialRefountRecordDetail detail=new MaterialRefountRecordDetail();
            detail.setMateId(Integer.parseInt(d.get("mateTypeId").toString()));
            detail.setRefoundCount(Integer.parseInt(d.get("outstorageCount").toString()));
            detail.setPrice(new BigDecimal(d.get("price").toString()));
            detail.setSumAmount(new BigDecimal(detail.getRefoundCount()).multiply(detail.getPrice()));
            detailList.add(detail);
        });

        service.saveRefoundRecord(record,detailList);
        return ResultTool.success();
    }
}
